package com.wsoft.bpm.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.bpm.entity.ActApprovalMessageEntity;
import com.wsoft.bpm.mapper.ActApprovalMessageMapper;
import com.wsoft.bpm.query.run.ApprovalMessageQuery;
import com.wsoft.bpm.service.IActApprovalMessageService;
import com.wsoft.bpm.vo.ApprovalMessageVO;
import com.wsoft.constant.CommonConstant;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.entity.BaseEntity;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 工作流审批语表 服务实现类
 * </p>
 *
 * @author zhonglj
 * @since 2024年12月21日 15:48:58
 */
@Service
public class ActApprovalMessageServiceImpl extends ServiceImpl<ActApprovalMessageMapper, ActApprovalMessageEntity> implements IActApprovalMessageService {

    @Override
    public void insert(ApprovalMessageQuery query) {
        ActApprovalMessageEntity entity = Convert.convert(ActApprovalMessageEntity.class, query);
        entity.setMemberId(LoginUserUtil.getUserInfo().getLoginUserVO().getId());
        save(entity);
    }

    @Override
    public void update(ApprovalMessageQuery query) {
        ActApprovalMessageEntity update = Convert.convert(ActApprovalMessageEntity.class, query);
        updateById(update);
    }

    @Override
    public void delete(Long id) {
        removeById(id);
    }

    @Override
    public IPage<ApprovalMessageVO> pageList(ApprovalMessageQuery query) {
        Page<ActApprovalMessageEntity> page = page(new Page<>(query.getPageNum(), query.getPageSize()),
                Wrappers.lambdaQuery(ActApprovalMessageEntity.class)
                        .eq(BaseEntity::getCreateBy, LoginUserUtil.getUserInfo().getLoginUserVO().getId())
                        .or()
                        .eq(ActApprovalMessageEntity::getIsSystem, CommonConstant.YESNO_YES)
                        .orderByDesc(ActApprovalMessageEntity::getIsSystem)
                        .orderByAsc(ActApprovalMessageEntity::getSort)
                        .orderByAsc(BaseEntity::getId)
        );

        return page.convert(e -> Convert.convert(ApprovalMessageVO.class, e));
    }

    @Override
    public ApprovalMessageVO detail(Long id) {
        ActApprovalMessageEntity detail = getById(id);
        if (detail == null) {
            return new ApprovalMessageVO();
        }
        
        return Convert.convert(ApprovalMessageVO.class, detail);
    }
}
